/**************************************************************************************
 *  Copyright (c)  2010  Leszek Karcz, Changmin Bae.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package data.missions;
import data.*;
import data.tileables.*;

/**************************************************************************************
 * Author(s): Changmin Bae
 * MoveToRally command
 **************************************************************************************/
public class MoveToCursor extends Mission{
	private Map map;
	private Player player;
	private int[] destCoords;
	private LinkedList<Integer> path;
	private Unit thisUnit;
	private int ticksTilMove;
	/**************************************************************************************
	 * Author(s): Changmin Bae
	 * MoveToRally constructor
	 **************************************************************************************/
	public MoveToCursor(Unit c, Map m, Player p, int x, int y, int move)
	{
		displayName = "Moving";
		destCoords = new int[2];
		map = m;
		player = p;
		thisUnit = c;
		destCoords[0] = x;
		destCoords[1] = y;
		ticksTilMove = move;
	}
	/**************************************************************************************
	 * Author(s): Changmin Bae
	 * MoveToRally execution
	 **************************************************************************************/
	public int execute()
	{
		if(ticksTilMove == 0)
		{
			ticksTilMove = thisUnit.getMovementTicks();
			if(path == null)
			{
				path = map.moveControllable(thisUnit, destCoords[0], destCoords[1], path);
				if(path==null)
				{
					return 1;
				}
				else
				{
					return 0;
				}
			}
			else if(path.getSize() > 0)
			{
				path = map.moveControllable(thisUnit, destCoords[0], destCoords[1], path);
				return 0;
			}
			else
			{
				return 1;
			}
		}
		ticksTilMove--;
		return 0;		
	}

}
